package org.jeecg.modules.publish.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 重置accseeToken,api,jsApi
 * @author 18534
 *
 */
public class AccessTokenUtil {
	private final static Logger LOG = LoggerFactory.getLogger(AccessTokenUtil.class);
	private final static String tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
	private static final String api_ticket_url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card";
	private static final String jsapi_ticket_url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
	private static final String APPID = "wxaff0e761caad8a3c";
	private static final String SECRET = "ca3d6064bbba7c33496ccb16e3bc4d7f";


	@Autowired
	private static RedisUtil redisUtil;

	public  static String  getAccseeToken(String appid,String appsecret){
//		String accessToken = null !=redisUtil.get("wxAccessToken") ? redisUtil.get("wxAccessToken").toString() : "";
		String accessToken = "";
		try {
			if(StringUtils.isEmpty(accessToken)){
				JSONObject accessTokenJson = RestUtil.get(tokenUrl+"&appid="+APPID+"&secret="+SECRET);
				accessToken = accessTokenJson.getString("access_token");
//				redisUtil.set("wxAccessToken",accessToken,7000);
			}
			return accessToken;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("AccseeToken error={}.", new Object[]{e});
			return accessToken;
		}
	}

}
